package com.megvii.koala.util;

import android.app.Activity;
import android.os.Build;
import android.os.PowerManager;
import android.view.View;
import android.view.WindowManager;

/**
 * Created by zhengtao on 2017/2/4.
 */

public class ActivityUtil {
    private static long lastClick = 0;

    public static void hideNavigationBar(Activity activity) {
        int systemUiVisibility = 0;

        // Navigation bar hiding:  Backwards compatible to ICS.
        if (Build.VERSION.SDK_INT >= 14) {
            systemUiVisibility ^= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
            systemUiVisibility ^= View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
            systemUiVisibility ^= View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
        }

        // 全屏展示
        if (Build.VERSION.SDK_INT >= 16) {
            systemUiVisibility ^= View.SYSTEM_UI_FLAG_FULLSCREEN;
        }

        if (Build.VERSION.SDK_INT >= 18) {
            systemUiVisibility ^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
        }

        activity.getWindow().getDecorView().setSystemUiVisibility(systemUiVisibility);
    }

    //获取屏幕锁
    public static void requestWakeLock(Activity activity) {
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
        PowerManager pManager = ((PowerManager) activity.getSystemService(Activity.POWER_SERVICE));
        PowerManager.WakeLock mWakeLock = pManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK
                | PowerManager.ON_AFTER_RELEASE, "");
        if (!mWakeLock.isHeld()) {
            mWakeLock.acquire();
        }
    }

    public static boolean canClick() {
        if (System.currentTimeMillis() - lastClick >= 500) {
            lastClick = System.currentTimeMillis();
            return true;
        }
        return false;
    }
}
